package com.fly.config;

import com.fly.constants.StatusConstants;
import com.fly.tools.CourseTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfig {

    // 创建一个会话存储对象
    @Bean
    public ChatMemory chatMemory() {
        return MessageWindowChatMemory.builder()
                .maxMessages(20)
                .build();
    }

    // 创建一个本地内存向量数据库
    @Bean
    public VectorStore vectorStore(OpenAiEmbeddingModel embeddingModel) {
        return SimpleVectorStore
                .builder(embeddingModel)
                .build();
    }

    /**
     * 默认模型
     * @param model
     * @param chatMemory
     * @return
     */
   /* @Bean
    public ChatClient chatClient(OllamaChatModel model, ChatMemory chatMemory) {
        return ChatClient
                .builder(model)
                .defaultSystem("你是小乐乐，温柔体贴的AI助手")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        // 注册会话存储对象
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                ) // 添加会话日志
                .build();
    }*/

    /**
     * 多态模型
     *
     * @param model
     * @param chatMemory
     * @return
     */
    @Bean
    public ChatClient chatClient(OpenAiChatModel model, ChatMemory chatMemory) {
        return ChatClient
                .builder(model)
                // 修改模型
                .defaultOptions(ChatOptions.builder().model("qwen-omni-turbo").build())
//                .defaultSystem("你是小乐乐，温柔体贴的AI助手")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        // 注册会话存储对象
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                ) // 添加会话日志
                .build();
    }

    @Bean
    public ChatClient gameChatClient(
            OpenAiChatModel model,
            ChatMemory chatMemory) {
        return ChatClient
                .builder(model)
                .defaultSystem(StatusConstants.GAME_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        // 注册会话存储对象
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                ) // 添加会话日志
                .build();
    }

    @Bean
    public ChatClient serviceChatClient(
            OpenAiChatModel model,
            ChatMemory chatMemory,
            CourseTools courseTools
    ) {
        return ChatClient
                .builder(model)
                .defaultSystem(StatusConstants.CUSTOMER_SERVICE_SYSTEM)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        // 注册会话存储对象
                        MessageChatMemoryAdvisor.builder(chatMemory).build()) // 添加会话日志
                .defaultTools(courseTools)
                .build();
    }

    @Bean
    public ChatClient pdfChatClient(OpenAiChatModel model,
                                    ChatMemory chatMemory,
                                    VectorStore vectorStore) {
        return ChatClient
                .builder(model)
                .defaultSystem("你是小乐乐，温柔体贴的AI助手")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        // 注册会话存储对象
                        MessageChatMemoryAdvisor.builder(chatMemory).build(),
                        // 注册向量数据库调用对象
                        QuestionAnswerAdvisor.builder(vectorStore)
                                .searchRequest(SearchRequest.builder()
                                        .similarityThreshold(0.6)
                                        .topK(2)
                                        .build())
                                .build()
                ) // 添加会话日志
                .build();
    }

}
